package com.digital.welcome.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.welcome.entity.OperateLog;
import com.digital.welcome.entity.PageRequest;
import com.digital.welcome.mapper.OperateLogMapper;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.OperateLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class OperateLogServiceImpl extends ServiceImpl<OperateLogMapper, OperateLog> implements OperateLogService {
    @Autowired
    private OperateLogMapper operateLogMapper;

    @Override
    public void insertOperateLog(OperateLog operateLog) {
        operateLogMapper.insert(operateLog);
    }

    @Override
    public String findPage(PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<OperateLog> operateLogList = operateLogMapper.selectList(null);
        PageInfo<OperateLog> pageInfo = new PageInfo<>(operateLogList);
        return JSON.toJSONString(new Result<>().setCode(1).setMessage("查询成功").setData(pageInfo));
    }

    @Override
    public String delete(OperateLog operateLog) {
        if (operateLog == null) {
            return JSON.toJSONString(new Result<>().setCode(0).setMessage("信息错误"));
        }
        operateLogMapper.deleteById(operateLog.getId());
        return JSON.toJSONString(new Result<>().setCode(1).setMessage("操作日志删除成功"));
    }

    @Override
    public String selectLog(PageRequest pageRequest) {
        if (pageRequest == null) {
            return JSON.toJSONString(new Result<>().setCode(0).setMessage("信息错误"));
        }
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        QueryWrapper<OperateLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("time", pageRequest.getLike());
        List<OperateLog> operateLogList = operateLogMapper.selectList(queryWrapper);
        PageInfo<OperateLog> pageInfo = new PageInfo<>(operateLogList);
        return JSON.toJSONString(new Result<>().setCode(1).setMessage("查询成功").setData(pageInfo));
    }

}
